package com.company.gxcpc2020;

import java.util.Scanner;
import java.util.stream.Stream;

public class GxcpcK {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        while (t-- > 0) {
            int line;
            line = sc.nextInt();
            Entity[] as = Stream.generate(() -> new Entity(sc.next().charAt(0), sc.nextInt())).limit(line).toArray(Entity[]::new);
            line = sc.nextInt();
            Entity[] bs = Stream.generate(() -> new Entity(sc.next().charAt(0), sc.nextInt())).limit(line).toArray(Entity[]::new);
            boolean flag = true;
            int l = 0;

            a:
            for (Entity a : as) {
                int v = 0;
                while (v < a.len) {
                    if (bs.length == l) {
                        flag = false;
                        break a;
                    }
                    Entity b = bs[l];
                    if (b.c == a.c) {
                        v += b.len;
                    }
                    l++;
                }
            }
            System.out.println(flag ? "Yes" : "No");

        }
    }

    public static class Entity {
        public char c;
        public int len;

        public Entity(char c, int len) {
            this.c = c;
            this.len = len;
        }
    }
}
